﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EFJson.Sample.Models;

namespace EFJson.Sample.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetOneTableData()
        {
            using (NorthwindEntities entities = new NorthwindEntities())
            {
                var query = entities.Categories.OrderBy(c => c.CategoryName).Take(20).ToList();
                var jsonObjects = query.Select(c => JsonHelper.ConvertToSimpleObject(c));
                return Json(jsonObjects, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult OneRelationship()
        {
            return View();
        }

        public ActionResult GetOneRelationshipData()
        {
            using (NorthwindEntities entities = new NorthwindEntities())
            {
                var query = entities.Products.Include("Categories").OrderBy(c => c.ProductName).Take(20).ToList();
                var jsonObjects = query.Select(c => JsonHelper.ConvertToSimpleObject(c, "Categories"));
                return Json(jsonObjects, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult ManyRelationship()
        {
            return View();
        }

        public ActionResult GetManyRelationshipData()
        {
            using (NorthwindEntities entities = new NorthwindEntities())
            {
                var query = entities.Suppliers.Include("Products").OrderBy(c => c.CompanyName).Take(4).ToList();
                var jsonObjects = query.Select(c => JsonHelper.ConvertToSimpleObject(c, "Products"));
                return Json(jsonObjects, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult MultiplePaths()
        {
            return View();
        }

        public ActionResult GetMultiplePathsData()
        {
            using (NorthwindEntities entities = new NorthwindEntities())
            {
                var query = entities.Suppliers.Include("Products.Categories").OrderBy(c => c.CompanyName).Take(4).ToList();
                var jsonObjects = query.Select(c => JsonHelper.ConvertToSimpleObject(c, "Products.Categories"));
                return Json(jsonObjects, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
